﻿create procedure [dbo].[usp_ProcessInsert]
	@BasePriority int
	, @ExitCode int
	, @ExitTime datetime2
	, @HasExited bit
	, @HandleCount int
	, @Id int
	, @MachineName nvarchar(100)
	, @MainModule nvarchar(512)
	, @MainWindowTitle nvarchar(100)
	, @MaxWorkingSet bigint
	, @MinWorkingSet bigint
	, @NonpagedSystemMemorySize64 bigint
	, @PagedMemorySize64 bigint
	, @PagedSystemMemorySize64 bigint
	, @PeakPagedMemorySize64 bigint
	, @PeakVirtualMemorySize64 bigint
	, @PeakWorkingSet64 bigint
	, @PriorityBoostEnabled bit
	, @PriorityClass nvarchar(100)
	, @PrivateMemorySize64 bigint
	, @PrivilegedProcessorTime time
	, @ProcessName nvarchar(100)
	, @ProcessorAffinity bigint
	, @Responding bit
	, @SessionId int
	, @StartTime datetime2
	, @Threads int
	, @TotalProcessorTime time
	, @UserProcessorTime time
	, @VirtualMemorySize64 bigint
	, @WorkingSet64 bigint
as

begin try
	
	begin transaction
	
		if not exists(select * from [dbo].[Process] where [BasePriority] = @BasePriority and
															([ExitCode] = @ExitCode or ([ExitCode] is null and @ExitCode is null)) and
															([ExitTime] = @ExitTime or ([ExitTime] is null and @ExitTime is null)) and
															[HasExited] = @HasExited and
															[HandleCount] = @HandleCount and
															[Id] = @Id and
															[MachineName] = @MachineName and
															[MainModule] = @MainModule and
															([MainWindowTitle] = @MainWindowTitle or ([MainWindowTitle] is null and @MainWindowTitle is null)) and
															[MaxWorkingSet] = @MaxWorkingSet and
															[MinWorkingSet] = @MinWorkingSet and
															[NonpagedSystemMemorySize64] = @NonpagedSystemMemorySize64 and
															[PagedMemorySize64] = @PagedMemorySize64 and
															[PagedSystemMemorySize64] = @PagedSystemMemorySize64 and
															[PeakPagedMemorySize64] = @PeakPagedMemorySize64 and
															[PeakVirtualMemorySize64] = @PeakVirtualMemorySize64 and
															[PeakWorkingSet64] = @PeakWorkingSet64 and
															[PriorityBoostEnabled] = @PriorityBoostEnabled and
															[PriorityClass] = @PriorityClass and
															[PrivateMemorySize64] = @PrivateMemorySize64 and
															[PrivilegedProcessorTime] = @PrivilegedProcessorTime and
															[ProcessName] = @ProcessName and
															[ProcessorAffinity] = @ProcessorAffinity and
															[Responding] = @Responding and
															[SessionId] = @SessionId and
															[StartTime] = @StartTime and
															[Threads] = @Threads and
															[TotalProcessorTime] = @TotalProcessorTime and
															[UserProcessorTime] = @UserProcessorTime and
															[VirtualMemorySize64] = @VirtualMemorySize64 and
															[WorkingSet64] = @WorkingSet64)
		
			begin
										                              
				insert [dbo].[Process]
				(
					[BasePriority]
					, [ExitCode]
					, [ExitTime]
					, [HasExited]
					, [HandleCount]
					, [Id]
					, [MachineName]
					, [MainModule]
					, [MainWindowTitle]
					, [MaxWorkingSet]
					, [MinWorkingSet]
					, [NonpagedSystemMemorySize64]
					, [PagedMemorySize64]
					, [PagedSystemMemorySize64]
					, [PeakPagedMemorySize64]
					, [PeakVirtualMemorySize64]
					, [PeakWorkingSet64]
					, [PriorityBoostEnabled]
					, [PriorityClass]
					, [PrivateMemorySize64]
					, [PrivilegedProcessorTime]
					, [ProcessName]
					, [ProcessorAffinity]
					, [Responding]
					, [SessionId]
					, [StartTime]
					, [Threads]
					, [TotalProcessorTime]
					, [UserProcessorTime]
					, [VirtualMemorySize64]
					, [WorkingSet64]
				)
				values
				(
					@BasePriority
					, @ExitCode
					, @ExitTime
					, @HasExited
					, @HandleCount
					, @Id
					, @MachineName
					, @MainModule
					, @MainWindowTitle
					, @MaxWorkingSet
					, @MinWorkingSet
					, @NonpagedSystemMemorySize64
					, @PagedMemorySize64
					, @PagedSystemMemorySize64
					, @PeakPagedMemorySize64
					, @PeakVirtualMemorySize64
					, @PeakWorkingSet64
					, @PriorityBoostEnabled
					, @PriorityClass
					, @PrivateMemorySize64
					, @PrivilegedProcessorTime
					, @ProcessName
					, @ProcessorAffinity
					, @Responding
					, @SessionId
					, @StartTime
					, @Threads
					, @TotalProcessorTime
					, @UserProcessorTime
					, @VirtualMemorySize64
					, @WorkingSet64
				)
				
				select ProcessId, ConcurrencyId, Created, Updated from [dbo].[Process] where @@rowcount > 0 and ProcessId = scope_identity()
			
			end
			
		else
			
			begin
			
				select ProcessId, ConcurrencyId, Created, Updated from [dbo].[Process] where [BasePriority] = @BasePriority and
																							 ([ExitCode] = @ExitCode or ([ExitCode] is null and @ExitCode is null)) and
																							 ([ExitTime] = @ExitTime or ([ExitTime] is null and @ExitTime is null)) and
																							 [HasExited] = @HasExited and
																							 [HandleCount] = @HandleCount and
																							 [Id] = @Id and
																							 [MachineName] = @MachineName and
																							 [MainModule] = @MainModule and
																							 ([MainWindowTitle] = @MainWindowTitle or ([MainWindowTitle] is null and @MainWindowTitle is null)) and
																							 [MaxWorkingSet] = @MaxWorkingSet and
																							 [MinWorkingSet] = @MinWorkingSet and
																							 [NonpagedSystemMemorySize64] = @NonpagedSystemMemorySize64 and
																							 [PagedMemorySize64] = @PagedMemorySize64 and
																							 [PagedSystemMemorySize64] = @PagedSystemMemorySize64 and
																							 [PeakPagedMemorySize64] = @PeakPagedMemorySize64 and
																							 [PeakVirtualMemorySize64] = @PeakVirtualMemorySize64 and
																							 [PeakWorkingSet64] = @PeakWorkingSet64 and
																							 [PriorityBoostEnabled] = @PriorityBoostEnabled and
																							 [PriorityClass] = @PriorityClass and
																							 [PrivateMemorySize64] = @PrivateMemorySize64 and
																							 [PrivilegedProcessorTime] = @PrivilegedProcessorTime and
																							 [ProcessName] = @ProcessName and
																							 [ProcessorAffinity] = @ProcessorAffinity and
																							 [Responding] = @Responding and
																							 [SessionId] = @SessionId and
																							 [StartTime] = @StartTime and
																							 [Threads] = @Threads and
																							 [TotalProcessorTime] = @TotalProcessorTime and
																							 [UserProcessorTime] = @UserProcessorTime and
																							 [VirtualMemorySize64] = @VirtualMemorySize64 and
																							 [WorkingSet64] = @WorkingSet64
			end
	
	commit transaction

end try
	
begin catch
	if @@trancount > 0
	begin
		rollback transaction;
	end
	execute [dbo].[usp_SqlErrorInsert]
end catch


